package com.cnpc.iot.scene.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnpc.framework.annotation.RefreshCSRFToken;
import com.cnpc.framework.annotation.VerifyCSRFToken;
import com.cnpc.framework.base.pojo.Result;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.utils.StrUtil;
import com.cnpc.iot.scene.entity.Device;
import com.cnpc.iot.scene.entity.MapSetting;

/**
* 地图设置管理控制器
* @author jrn
* 2018-05-12 14:18:51由代码生成器自动生成
*/
@Controller
@RequestMapping("/mapsetting")
public class MapSettingController {

    @Resource
    private BaseService baseService;

    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String list(){
        return "iot/scene/mapsetting_list";
    }
    
    @RequestMapping(value="/showmap/{id}",method = RequestMethod.GET)
    public String showmap(@PathVariable("id") String id, HttpServletRequest request){
    	MapSetting ms = baseService.get(MapSetting.class, id);
    	request.setAttribute("map", ms);
        return "iot/data/showmap";
    }

    @RefreshCSRFToken
    @RequestMapping(value="/edit",method = RequestMethod.GET)
    public String edit(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "iot/scene/mapsetting_edit";
    }

    @RequestMapping(value="/detail",method = RequestMethod.GET)
    public String detail(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "iot/scene/mapsetting_detail";
    }

    @RequestMapping(value="/get/{id}",method = RequestMethod.POST)
    @ResponseBody
    public MapSetting get(@PathVariable("id") String id){
        return baseService.get(MapSetting.class, id);
    }
    
    @RequestMapping(value="/getMarker/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result getMarker(@PathVariable("orgId") String orgId){
    	List<Device> devices = baseService.find("from Device where orgid='"+orgId+"'");
    	if(devices.isEmpty()) {
    		return new Result(false);
    	}else {
    		Result result = new Result();
    		result.setData(devices);
    		return result;
    	}
    }
    
    @RequestMapping(value="/getByOrgId/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result getByOrgId(@PathVariable("orgId") String orgId){
    	List<MapSetting> map = baseService.find("from MapSetting where orgid='"+orgId+"'");
    	if(map.isEmpty()) {
    		return new Result(false);
    	}else {
    		Result result = new Result();
    		result.setData(map.get(0));
    		return result;
    	}
    }

    @VerifyCSRFToken
    @RequestMapping(value="/save")
    @ResponseBody
    public Result save(MapSetting mapsetting){
        if(StrUtil.isEmpty(mapsetting.getId())){
            baseService.save(mapsetting);
        }
        else{
            mapsetting.setUpdateDateTime(new Date());
            baseService.update(mapsetting);
        }
        return new Result(true);
    }



    @RequestMapping(value="/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") String id){
        MapSetting mapsetting=this.get(id);
        try{
            baseService.delete(mapsetting);
            return new Result();
        }
        catch(Exception e){
            return new Result(false,"该数据已经被引用，不可删除");
        }
    }



}
